package kassiopeia.agent.alerting;

import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AlertSender {

    private String mailHost;
    private String mailPort;
    private boolean enabled;
    private String password;
    private String login;
    private String smtpAuth;

    @Value("${mail.smtp.host}")
    public void setMailHost(String mailHost) {
        this.mailHost = mailHost;
    }

    @Value("${mail.smtp.port}")
    public void setMailPort(String mailPort) {
        this.mailPort = mailPort;
    }

    @Value("${mail.enabled}")
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Value("${mail.login}")
    public void setLogin(String login) {
        this.login = login;
    }

    @Value("${mail.password}")
    public void setPassword(String password) {
        this.password = password;
    }

    @Value("${mail.smtp.auth}")
    public void setSmtpAuth(String smtpAuth) {
        this.smtpAuth = smtpAuth;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void send(String subject, List<String> sendTo, String body,
            boolean priorityHi, DataSource... attachements)
            throws MessagingException {

        Session session = Session.getInstance(createMessageProperties(), new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(login, password);
            }
        });
        
        MimeMessage message = createMessage(priorityHi, session, body, sendTo, subject, attachements);
        Transport.send(message);
    }

    private Properties createMessageProperties() {
        Properties props = new Properties();
        props.put("mail.smtp.host", mailHost);
        props.put("mail.smtp.auth", smtpAuth);
        props.put("mail.smtp.port", mailPort);
        return props;
    }

    private MimeMessage createMessage(boolean priorityHi, Session session,
            String body, List<String> sendTo,
            String subject, DataSource ...attachements) throws MessagingException {

        MimeMessage message = new MimeMessage(session);
        if (priorityHi) {
            message.setHeader("X-Priority", "1");
        }
        Multipart multipart = new MimeMultipart();
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(body);
        multipart.addBodyPart(messagePart);

        for (String address : sendTo) {
            InternetAddress iaddress = new InternetAddress(address);
            message.addRecipient(Message.RecipientType.TO, iaddress);
        }
        message.setSubject(subject);
        for (DataSource attachement : attachements) {
            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setFileName(attachement.getName());
            attachmentPart.setDataHandler(new DataHandler(attachement));
            multipart.addBodyPart(attachmentPart);
        }

        message.setContent(multipart);
        return message;
    }
}
